<include file="public@header"/>

<body>
<div class="wrap js-check-wrap">
    <ul class="nav nav-tabs">
        <li><a href="{:url('Message/index')}">发送消息列表</a></li>
        <li class="active"><a href="{:url('Message/sendToParents')}">给家长发送通知</a></li>
    </ul>

    <div class="row margin-top-20 well form-inline">
        <label class="form-group col-lg-3 col-md-4">学校:
            <select class="form-control" name="school_id" id="school_id" style="width: 179px;">
                <option value='0'>全部</option>
                <notempty name="school">
                    <volist name="school" id="vo">
                        <if condition="isset($param.school_id) && $param.school_id == $vo.id">
                            <option value="{$vo.id}" selected>{$vo.name}</option>
                        <else/>
                            <option value="{$vo.id}">{$vo.name}</option>
                        </if>
                    </volist>
                </notempty>
            </select>
        </label>
        <label class="form-group col-lg-3 col-md-4">课程:
            <select class="form-control" name="course_id" id="course_id" style="width: 179px;">
                <option value='0'>全部</option>
                <notempty name="course">
                    <volist name="course" id="vo">
                        <option value="{$vo.id}">{$vo.course_name}</option>
                    </volist>
                </notempty>
            </select>
        </label>
        <label class="form-group col-lg-3 col-md-4">班级:
            <select class="form-control" name="class_id" id="class_id" style="width: 179px;">
                <option value='0'>全部</option>
            </select>
        </label>

        <button type="submit" id="send_select"  class="btn btn-primary js-ajax-submit">筛选</button>
        <a class="btn btn-danger" href="{:url('Message/sendToParents')}">清空</a>
        <div id="error_msg" style="color: red;"></div>
    </div>

    <div>共&nbsp;<span id="parent_total" style="color: red;">0</span>&nbsp;条记录</div>
    
    <div id="form_send" class="row margin-top-20" style="display: none;">
        <div class="col-md-12 col-lg-10">
            <form action="{:url('Message/sendPost')}" method="post" class="form-horizontal js-ajax-form margin-top-20">
                <input type="hidden" name="send_school_id" id="send_school_id" value="0">
                <input type="hidden" name="send_course_id" id="send_course_id" value="0">
                <input type="hidden" name="send_class_id" id="send_class_id" value="0">
                <div class="row">
                    <div class="col-md-12">
                        <table class="table table-bordered chase_classroom_table">
                            <tr>
                                <th>类型<span class="form-required">*</span></th>
                                <td>
                                    <select class="form-control" name="type" id="input-type" style="width: 240px;">
                                        <option value="0" selected>站内信</option>
                                        <option value="1">短信</option>
                                        <option value="2">邮件</option>
                                    </select>
                                </td>
                            </tr>
                            <tr id="subject">
                                <th>主题<span class="form-required">*</span></th>
                                <td>
                                    <input type="text" class="form-control" name="subject" placeholder="请输入主题" value="">
                                </td>
                            </tr>
                            <tr>
                                <th>内容<span class="form-required">*</span></th>
                                <td>
                                    <textarea class="form-control" id="input-content" name="content" rows="8"></textarea>
                                </td>
                            </tr>
                        </table>

                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <button type="submit" class="btn btn-primary js-ajax-submit">确认发送</button>
                            </div>
                        </div>
                    </div>
                </div>
            </form>

        </div>
    </div>
</div>
<!--ajax layer 弹窗-->
<script type="text/javascript" src="__STATIC__/js/admin.js"></script>

<script type="text/javascript">
    $(document).ready(function() {
        //筛选班级
        $('#school_id').on('change',function () {
            var sid = $(this).val();
            if(sid == 0){
                $('#class_id').empty();
                var html = '<option value=\"0\">全部</option>';
                $('#class_id').html(html);
                return false;
            }
            $.ajax({
                url:"{:url('Message/ajax_class')}",
                type:'post',
                dataType:'json',
                data:{"school_id":sid},
                success:function(jsonData){
                    $('#class_id').empty();
                    var html = '<option value=\"0\">全部</option>';

                    if(jsonData.code == 1){
                        for(var i in jsonData.data){
                            html += '<option value=\"'+jsonData.data[i].id+'\">'+jsonData.data[i].name+'</option>';
                        }
                    }
                    $('#class_id').html(html);
                },
                error:function(XMLHttpRequest, textStatus, errorThrown){
                    console.log('xhr='+XMLHttpRequest.readyState+' status='+textStatus+' error='+errorThrown);
                }
            });
        });

        //发送消息类型
        $('#input-type').on('change', function () {
            var type_id = $('#input-type').val();
            //清除主题信息
            $("input[name=subject]").val('');

            if(type_id == 1){
                //短信
                $('#subject').hide();
            }else{
                //站内信息或邮件
                $('#subject').show();
            }
        });

        //筛选家长
        $('#send_select').on('click',function () {
            var school_id = $('#school_id').val();
            var course_id = $('#course_id').val();
            var class_id = $('#class_id').val();
            if(school_id == 0){
//                $('#error_msg').text('请选择学校');
//                $('#error_msg').fadeIn(1);
//                $('#error_msg').fadeOut(2000);
//                return false;
            }else{
                $('#error_msg').empty();
            }

            //保存隐藏字段值
            $('#send_school_id').val(school_id);
            $('#send_course_id').val(course_id);
            $('#send_class_id').val(class_id);

            $.ajax({
                url:"{:url('Message/ajaxParent')}",
                type:'post',
                dataType:'json',
                data:{"school_id":school_id, "class_id":class_id, "course_id":course_id},
                beforeSend:function () {
                    $('#form_send').hide();
                },
                success:function(jsonData){
                    if(jsonData.code == 1){
                        var total = jsonData.data.total;
                        $('#parent_total').text(total);
                        if(total > 0){
                            $('#form_send').show();
                        }
                    }else{
                        $('#error_msg').text(jsonData.msg);
                        $('#error_msg').fadeIn(1);
                        $('#error_msg').fadeOut(2000);
                    }

                },
                error:function(XMLHttpRequest, textStatus, errorThrown){
                    console.log('xhr='+XMLHttpRequest.readyState+' status='+textStatus+' error='+errorThrown);
                }
            });
        });
    });

</script>

</body>
